//
//  WTPlayToolView.m
//  WTPlayDemo
//
//  Created by ben on 16/7/15.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTPlayToolView.h"
#import "Masonry.h"
#import "WTCategories.h"

@interface WTPlayToolView ()
@property (strong, nonatomic) UIButton *chatButton;
@property (strong, nonatomic) UIButton *messageButton;
@property (strong, nonatomic) UIButton *pushButton;
@property (strong, nonatomic) UIButton *shareButton;
@property (strong, nonatomic) UIButton *giftButton;
@property (strong, nonatomic) UIView*   redPointView;
@end


@implementation WTPlayToolView

- (void)showOrHide{
    
    NSInteger alpha = 1;
    if(self.chatButton.alpha == 1){
        alpha = 0;
        [self.pushButton setImage:[UIImage wt_imageWithName:@"wt_play_pull_icon"] forState:UIControlStateNormal];
        self.backgroundColor = [UIColor clearColor];
    }else {
        [self.pushButton setImage:[UIImage wt_imageWithName:@"wt_play_push_icon"] forState:UIControlStateNormal];
    }
    self.messageButton.alpha = alpha;
    self.giftButton.alpha = alpha;
    self.shareButton.alpha = alpha;
    self.chatButton.alpha = alpha;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGRect messageButtonFrame = self.messageButton.frame;
    CGFloat x = messageButtonFrame.size.width / 2 + 5;
    self.redPointView.x = x;
    self.redPointView.y = (messageButtonFrame.size.height) / 3 - 5;
    self.redPointView.size = CGSizeMake(10, 10);
    [self.messageButton addSubview:self.redPointView];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}


#pragma mark setup
- (void)setup{
    
    __weak __typeof(self)weakSelf = self;
    self.chatButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage wt_imageWithName:@"wt_play_chat_icon"] forState:UIControlStateNormal];
        [button addAction:^{
            if(weakSelf.itemPressedBlock){
                weakSelf.itemPressedBlock(0);
            }
        }];
        [self addSubview:button];
        button;
    });
    
    self.messageButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage wt_imageWithName:@"wt_play_message_icon"] forState:UIControlStateNormal];
        [button addAction:^{
            if(weakSelf.itemPressedBlock){
                weakSelf.itemPressedBlock(1);
            }
        }];
        [self addSubview:button];
        button;
    });
    
    self.giftButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage wt_imageWithName:@"wt_play_gift_icon"] forState:UIControlStateNormal];
        [button addAction:^{
            if(weakSelf.itemPressedBlock){
                weakSelf.itemPressedBlock(2);
            }
        }];
        [self addSubview:button];
        button;
    });
    
    self.shareButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage wt_imageWithName:@"wt_play_share_icon"] forState:UIControlStateNormal];
        [button addAction:^{
            if(weakSelf.itemPressedBlock){
                weakSelf.itemPressedBlock(3);
            }
        }];
        [self addSubview:button];
        button;
    });
    
    self.pushButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage wt_imageWithName:@"wt_play_push_icon"] forState:UIControlStateNormal];
        [button addAction:^{
            if(weakSelf.itemPressedBlock){
                weakSelf.itemPressedBlock(4);
            }
        }];
        [self addSubview:button];
        button;
    });
    
    self.redPointView = ({
        
        UIView* view = [[UIView alloc] init];
        view.backgroundColor = [UIColor themeColor];
        view.layer.cornerRadius = 5;
        view.layer.masksToBounds = YES;
        view.hidden = YES;
        [self.messageButton addSubview:view];
        view;
    });
}


- (void)setIsLivePage:(BOOL)isLivePage{
    
    _isLivePage = isLivePage;
    CGFloat multiplier = 0.2;
    if(isLivePage){
        multiplier = 0.25;
    }
    [self.chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.top.and.bottom.mas_equalTo(0);
        make.width.equalTo(self).multipliedBy(multiplier);
    }];
    [self.messageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(self.chatButton.mas_trailing);
        make.width.equalTo(self).multipliedBy(multiplier);
    }];
    
    if(isLivePage){
        [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.mas_equalTo(0);
            make.leading.mas_equalTo(self.messageButton.mas_trailing);
            make.width.equalTo(self).multipliedBy(multiplier);
        }];
        [self.pushButton setImage:[UIImage wt_imageWithName:@"wt_play_change_camera"] forState:UIControlStateNormal];
    }else {
        [self.giftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.mas_equalTo(0);
            make.leading.mas_equalTo(self.messageButton.mas_trailing);
            make.width.equalTo(self).multipliedBy(multiplier);
        }];
        [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.mas_equalTo(0);
            make.leading.mas_equalTo(self.giftButton.mas_trailing);
            make.width.equalTo(self).multipliedBy(multiplier);
        }];
    }
    [self.pushButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(self.shareButton.mas_trailing);
        make.trailing.mas_equalTo(0);
    }];
}
@end
